<script lang="ts" setup name="HomeHot">
import useStore from '@/store'
import { useIntersectionObserver } from '@vueuse/core'
import { storeToRefs } from 'pinia'
import { ref } from 'vue'
import HomePanel from './home-panel.vue'
import { useLazyData } from '@/hooks'
import HomeSkeleton from './home-skeleton.vue'
const { home } = useStore()
const { getGoodsHotList } = home
const { hotGoodsList } = storeToRefs(home)
// #1 注释掉
// getGoodsHotList()

const target = useLazyData(() => {
  setTimeout(() => getGoodsHotList(), 2000)
})
// #2 观察 target 是否进入可视区
/* const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {
  // 如果观测的目标 target 到达了可视区 isIntersecting 就是 true
  if (isIntersecting) {
    // #3 采取调用我的接口，拿数据
    getGoodsHotList()
    stop()
  }
}) */
</script>
<template>
  <div class="home-hot">
    <HomePanel title="人气推荐" sub-title="人气爆款 不容错过" ref="target">
      <template #right><XtxMore path="/" /></template>
      <!-- 面板内容 -->
      <transition name="fade">
        <ul class="goods-list" v-if="hotGoodsList.length > 0">
          <li v-for="item in hotGoodsList" :key="item.id">
            <RouterLink to="/">
              <img v-lazy="item.picture" alt="" />
              <p class="name ellipsis">{{ item.title }}</p>
              <p class="price">{{ item.alt }}</p>
            </RouterLink>
          </li>
        </ul>
        <HomeSkeleton v-else />
      </transition>
    </HomePanel>
  </div>
</template>

<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    .hoverShadow();
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding-top: 12px;
      text-align: center;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
</style>
